﻿#region XNA Libraries

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#endregion

#region SDK Libraries

using main.sdk.audio;
using main.sdk.display;
using main.sdk.display.object2d;
using main.sdk.input;
using main.sdk.scene;

#endregion

namespace main.src.scene
{

    public sealed class Scene_Title : Scene_Base
    {

        private readonly Sprite Background = new Sprite("rsc/picture/Title");

        private readonly Label Title = new Label("Title");

        private const float SCALE_INITIAL = 4;
        private const float SCALE_FACTOR  = 0.25f;

        private readonly Label[] Commands = {
            new Label("New Game"),
            new Label("Exit")
        };

        private const byte  ALPHA_GAIN = 5;
        private const byte  ALPHA_LOSS = 5;
        private const float SCALE_FINAL = 2;

        private byte Step  = 0;
        private byte Index = 0;
        private Scene_Base NextScene;

        public override void Start()
        {
            Background.Alpha = 0;

            Title.Scale.X = SCALE_INITIAL;
            Title.Scale.Y = SCALE_INITIAL;
            Title.OCenter();
            Title.XO = (Program.Width - Title.Width) / 2;
            Title.YO = Program.Height / 4 - Title.Height / 2;
            Title.Outline = true;
            Title.OutlineSize = 2;
            Title.Visible = false;

            for (byte i = 0; i < Commands.Length; i++)
            {
                Commands[i].OCenter();
                Commands[i].XO = (Program.Width - Commands[i].Width) / 2;
                Commands[i].YO = Program.Height - (Commands.Length - i) * Commands[i].Height - 32;
                Commands[i].Outline = true;
                Commands[i].OutlineSize = 2;
                Commands[i].Alpha = 0;
            }
        }

        public override void Update()
        {
            switch (Step)
            {
                case 0:
                    Background.Alpha += ALPHA_GAIN;
                    if (Background.Alpha == 255)
                    {
                        Title.Visible = true;
                        Step = 1;
                    }
                    break;
                case 1:
                    Title.Scale.X -= SCALE_FACTOR;
                    Title.Scale.Y -= SCALE_FACTOR;
                    if (Title.Scale.X <= 1)
                    {
                        Title.Scale.X = 1;
                        Title.Scale.Y = 1;
                        Step = 2;
                    }
                    break;
                case 2:
                    foreach (Label command in Commands)
                        command.Alpha += ALPHA_GAIN;
                    if (Commands[0].Alpha == 255)
                        Step = 3;
                    break;
                case 3:
                    for (byte i = 0; i < Commands.Length; i++)
                        if (i == Index)
                        {
                            Commands[i].Scale.X = Commands[i].Scale.X >= SCALE_FINAL ? SCALE_FINAL : Commands[i].Scale.X + SCALE_FACTOR;
                            Commands[i].Scale.Y = Commands[i].Scale.Y >= SCALE_FINAL ? SCALE_FINAL : Commands[i].Scale.Y + SCALE_FACTOR;
                        }
                        else
                        {
                            Commands[i].Scale.X = Commands[i].Scale.X <= 1 ? 1 : Commands[i].Scale.X - SCALE_FACTOR;
                            Commands[i].Scale.Y = Commands[i].Scale.Y <= 1 ? 1 : Commands[i].Scale.X - SCALE_FACTOR;
                        }
                    if (Input.Trigger(Keys.Down))
                    {
                        ++Index;
                        Index %= (byte)Commands.Length;
                    }
                    if (Input.Trigger(Keys.Up))
                        Index = Index <= 0 ? (byte)(Commands.Length - 1) : (byte)(Index - 1);
                    if (Input.Trigger(Keys.Enter))
                    {
                        switch (Index)
                        {
                            case 0:
                                NextScene = new Scene_Map();
                                break;
                        }
                        Step = 4;
                    }
                    break;
                case 4:
                    Title.Scale.X = Title.Scale.X <= 0 ? 0 : Title.Scale.X - SCALE_FACTOR;
                    Title.Scale.Y = Title.Scale.Y <= 0 ? 0 : Title.Scale.Y - SCALE_FACTOR;
                    foreach (Label command in Commands)
                    {
                        command.Scale.X = command.Scale.X <= 0 ? 0 : command.Scale.X - SCALE_FACTOR;
                        command.Scale.Y = command.Scale.Y <= 0 ? 0 : command.Scale.Y - SCALE_FACTOR;
                    }
                    if (Commands[Index].Scale.X == 0)
                        Step = 5;
                    break;
                case 5:
                    Background.Alpha -= ALPHA_LOSS;
                    if (Background.Alpha == 0)
                        Scene.Set(NextScene);
                    break;
            }
        }

        public override void Terminate()
        {
            Title.Dispose();
            foreach (Label command in Commands)
                command.Dispose();
        }

    }

}